﻿
Namespace internalFunctions

    'parameters, i, input object, pnt<T>
    'return, T, the instance in pnt<T>
    Public Class pntget
        Inherits internalFunction

        Public Class parameters
            Public Const i As String = "i"
        End Class

        Private Shared a As aliases = Nothing

        Public Sub New()
            MyBase.New("pointer.get", shared_parameters_mappings.mi, a)
        End Sub

        Private Class callback
            Inherits pd_t(Of Object)

            Protected Overrides Function cb(Of T)(ByRef i As pointer(Of T), ByRef rtn As Object, _
                                                  ByVal ParamArray ps() As Object) As Boolean
                If Not i Is Nothing Then
                    rtn = (+i)
                    Return True
                Else
                    Return False
                End If
            End Function
        End Class

        Private Shared cb As callback = Nothing

        Shared Sub New()
            cb = New callback()
            initialAliases(a, "pnt.get", "pnt.g")
        End Sub

        Protected Overrides Function invoke(ByVal ctx As context, ByRef output As bare_variable) As Boolean
            Dim i As bare_variable = Nothing
            i = ctx.findVariable(parameters.i)
            If i Is Nothing Then
                raiseError("expect parameter " + parameters.i, errorHandle.errorType.user)
                Return False
            Else
                Dim obj As Object = Nothing
                If i.invoke(obj, cb) Then
                    output = New bare_variable(i.variable().elementType1(), obj)
                    Return True
                Else
                    Return False
                End If
            End If
        End Function
    End Class

    'parameters, pnt, input object, pnt<T>
    '            i, any, match T, the one need to set to pnt
    'return, bool, whether the operation is success or not
    Public Class pntset
        Inherits internalFunction

        Public Class parameters
            Public Const pnt As String = "pnt"
            Public Const i As String = "i"
        End Class

        Private Shared a As aliases = Nothing
        Private Shared m As iParametersMapping = Nothing

        Public Sub New()
            MyBase.New("pointer.set", m, a)
        End Sub

        Private Class callback
            Inherits pd_t(Of Boolean)

            Public Overrides Function cb(Of T)(ByRef i As pointer(Of T), ByRef rtn As Boolean, _
                                               ByVal ET As typeDefination, ByVal ParamArray ps() As Object) As Boolean
                assert(Not ps Is Nothing AndAlso ps.Length = 1)
                If Not i Is Nothing AndAlso matchtype(ET, ps(0)) Then
                    i.setinstance(convertor.cast(Of T)(convertor.cast(Of bare_variable)(ps(0)).value()))
                    Return True
                Else
                    raiseError("expect input object is not nothing, and also match type.", _
                               errorHandle.errorType.user)
                    Return False
                End If
            End Function
        End Class

        Private Shared cb As callback = Nothing

        Shared Sub New()
            cb = New callback()
            initialAliases(a, "pnt.set", "pnt.s")
            initialMapping(m, parameters.pnt, parameters.i)
        End Sub

        Protected Overrides Function invoke(ByVal ctx As context, ByRef output As bare_variable) As Boolean
            Dim pnt As bare_variable = Nothing
            pnt = ctx.findVariable(parameters.pnt)
            If pnt Is Nothing Then
                raiseError("expect parameter " + parameters.pnt, errorHandle.errorType.user)
                Return False
            Else
                Dim i As bare_variable = Nothing
                i = ctx.findVariable(parameters.i)
                If i Is Nothing Then
                    raiseError("expect parameter " + parameters.i, errorHandle.errorType.user)
                    Return False
                Else
                    Dim rtn As Boolean
                    If pnt.invoke(rtn, cb, i) Then
                        output = New bare_variable(typeDefination.bool, rtn)
                        Return True
                    Else
                        Return False
                    End If
                End If
            End If
        End Function
    End Class
End Namespace